<template>
  <el-dialog :visible="dialogvisibale" @close="vclose" title="添加课时">
    <el-form :model="video" label-width="120px">
      <el-form-item label="课时标题">
        <el-input v-model="video.title"></el-input>
      </el-form-item>
      <el-form-item label="课时排序">
        <el-input-number v-model="video.sort" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="是否免费">
        <el-switch v-model="video.isFree"></el-switch>
      </el-form-item>
    <el-form-item label="上传视频">
      <el-upload 
       action="http://127.0.0.1:8130/admin/vod/media/upload"
       :auto-upload="false"
       :on-success="success"
       :on-error="error"
       :file-list="fileList"
       ref="upload"
       :before-upload="preupload"
       :limit="1"
       :on-exceed="overlimit"
       :on-remove="removeAliyun"
       >
        <el-button slot="trigger" style="margin-right:20px" type="primary">选择视频</el-button>
        <el-button type="success" @click="uploadVideo()">上传视频</el-button>
      </el-upload>
    </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button type="" @click="vclose">取消</el-button>
      <el-button type="primary" @click="addAndupdate()">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import VideoApi from "@/api/video";
export default {
  data() {
    return {
      dialogvisibale: false,
      video: {
        chapterId: null,
        courseId: null,
        videoId: null,
        sort: 0,
      },
      fileList:[]
    };
  },
  methods: {
    //云服务删除
    removeAliyun(files,fileList){
      if(!this.video.videoOriginalName){ //不是编辑状态不要删除
        return
      }
      else{
        VideoApi.removeAliyunVideo(this.video.videoSourceId).then(res=>{
           if(res.code===20000){ //云删除成功
            this.video.videoOriginalName ='';
            this.video.videoSourceId ='';
            this.$message.success("云视频删除成功");
            this.$refs.upload.clearFiles();
            VideoApi.updateVideo(this.video).then(res=>{
              this.$parent.fetchData(); //重刷
            })
           }
        })
      }
    },
    overlimit(files,fileList){
       this.$message.warning("只能上传一个视频"); 
    },
    preupload(file){
      const fileType = file.type;
      const fileSize = file.size;
      if (!fileType === "video/mp4") {
        this.$message.error("文件类型非法");
      }
      if (fileSize / 1024 / 1024 > 1000) {
        ths.$message.error("上传文件过大");
      }
    },
    success(response,file,fileList){
      if(response.success){
       this.$message.success("上传成功");
       this.video.videoSourceId=response.data.videoId;
       this.video.videoOriginalName=response.data.filename;
       this.$refs.upload.clearFiles();
       }
    },
    error(){
       this.$message.error("上传失败");
    },
    //视频上传
    uploadVideo(){
       this.$message.warning("请耐心等待，视频上传时间可能不一样，提示上传成功表示真正上传成功")
       this.$refs.upload.submit();
    },
    vopen(chapterId) {
      this.video.chapterId = chapterId;
      this.dialogvisibale = true;
      if (this.video.videoId != null) {
        //编辑状态
        VideoApi.selectVideo(this.video.videoId).then((res) => {
          this.video = res.data.item;
          //console.log(this.video.videoOriginalNam!='');
          if(this.video.videoOriginalName != ''){
          this.fileList=[{'name':this.video.videoOriginalName}]
          //console.log("......")
        }
        });
        
      } else {
        this.video.videoId = null; //确保后续判断
      }
    },
    edit(id) {
      //获取video id值
      //console.log(id);
      this.video.videoId = id;
    },
    vclose() {
      this.dialogvisibale = false;
      this.video = {
        sort: 0,
      };
      this.fileList=[];
    },
    removeVideo(id){
        this.$confirm("删除操作非常危险，确定继续？","删除提示",
         {
           confirmButtonText:'确认',
           cancelButtonText:'取消'
         }
        ).then(()=>{
            //删除操作
            VideoApi.removeVideoById(id).then(res=>{
              this.$message.success(res.message);
              this.$parent.fetchData(); //重刷
            })
        }).catch((err)=>{
          if(err=="cancel"){
            this.$message.error("取消删除")
          }
        })
    },
    addAndupdate() {
      console.log(this.video.videoId !== null);
      if (this.video.videoId !== null) {
        //编辑
        this.video.courseId = this.$parent.$parent.courseId;
        VideoApi.updateVideo(this.video).then((res) => {
          this.$message.success(res.message);
          this.vclose();
          this.$parent.fetchData(); //重刷
        });
      } else {
        //新增
        this.video.courseId = this.$parent.$parent.courseId;
        //console.log(this.video)
        VideoApi.addVideo(this.video).then((res) => {
          this.$message.success(res.message);
          this.vclose();
          this.$parent.fetchData(); //重刷
        });
      }
    },
  },
};
</script>